package com.zyx.kafkademo.consumer;

import com.alibaba.fastjson.JSONObject;
import com.zyx.kafkademo.utils.TimeUtils;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.time.Duration;
import java.util.Collections;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author Yaxi.Zhang
 * @since 2021/8/18 10:36
 * desc: 指定位置消费信息
 */
public class SeekDemo {
    public static final String BROKER_LIST = "bigdata022:9092,bigdata023:9092,bigdata024:9092";
    public static final String TOPIC = "tickerquotation";
    public static final String GROUP_ID = "test_consumer_210818";

    public static Properties initConfig() {
        Properties props = new Properties();
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,
                StringDeserializer.class.getName());
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,
                StringDeserializer.class.getName());
        props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "false");
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, BROKER_LIST);
        props.put(ConsumerConfig.GROUP_ID_CONFIG, GROUP_ID);
        return props;
    }

    public static void main(String[] args) {

        Properties props = initConfig();

        try (KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props)) {
            consumer.subscribe(Collections.singletonList(TOPIC));

            long start = System.currentTimeMillis();
            Set<TopicPartition> assignment = new HashSet<>();

            while (assignment.size() == 0) {
                // 如果没有分配到分区,就一直循环下去
                consumer.poll(Duration.ofMillis(100));
                assignment = consumer.assignment();
            }

            long end = System.currentTimeMillis();

            System.out.println("已获取主题分区信息,costing " + (end - start) + "ms...");
            System.out.println("主题分区信息:" + assignment);

            for (TopicPartition tp : assignment) {
                // 消费第当前分区的offset为指定值的消息
                consumer.seek(tp, 6000000);
            }

            AtomicInteger index = new AtomicInteger(0);

            while (true) {
                ConsumerRecords<String, String> records =
                        consumer.poll(Duration.ofMillis(1000));
                // 消费信息
                for (ConsumerRecord<String, String> record : records) {
                    String value = record.value();
                    JSONObject jsonObject = JSONObject.parseObject(value);
                    Long ts = jsonObject.getLong("time");
                    String formatTimeStr = TimeUtils.getFormatTimeStr(ts);
                    // 输出信息
                    System.out.println(record.offset() + ":" + record.value() + " ==> " + formatTimeStr);
                }

                int cur = index.incrementAndGet();
                if (cur >= 20) {
                    break;
                }
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }

    }

}
